<?php

namespace app\adminmall\controller;
use app\adminmall\model\AboutModel;
use app\common\controller\AdminBaseController;
use think\Db;
/**
 * 软文推广
 */
class AboutController extends AdminBaseController
{
 	 protected $model;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new AboutModel();
    }
	
    /**
     * 软文推广列表
     * @author [longx] 
     */
    public function index($page = 1){
    	$where=[];
		if(request()->isPost()){
	    	$param = input('post.');
			//标题
	    	if($param['title']){
	    	 	 $where['title'] = ['like', '%'.$param['title'].'%'];
	    	}
	    	//店铺名称		
	    	if($param['store_name']){
	    	  	$where['store_name'] = ['like', '%'.$param['store_name'].'%'];
	    	}
	    	//时间
	    	if($param['order_time'] && $param['end_time']){
				$where['addtime'] = ['between time', [$param['order_time'],$param['end_time']]];
			}
		}
    	$list=Db::name('action_about')
	 	->alias('a')
	 	->join('mall_agent b','a.agent_id=b.id')
	 	->field('a.*,b.store_name')
	 	->where($where)
	 	->paginate(10);  
    	return $this->fetch('index',['list' =>$list ]);
    }
    /**
     * 
     *添加
     * @author [longx] 
     */
    public function add()
    {
        if(request()->isAjax()){
        	$param=input('post.');
			$param['addtime'] = now_time();
			if ($this->model->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('News/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [lx]
     */
    public function edit($id)
    {
        if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('News/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }



    /**
     * [del删除]
     * @author [lx] 
     */
    public function delete()
    {
        $id = input('param.id');

        if (Db::name('action_news')->where('id',$id)->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}